import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.*;


public class YT1 extends SeleneseTestCase {
	public int xR_TD, xC_TD;
	public String xTD_D[][];
	@BeforeTest
	public void setUp() throws Exception {
		xlRead_TD("C:/Recordings/Selenium Recordings/PL10-TestNG/YT_KeywordStrength1.xls");
		selenium = new DefaultSelenium("localhost", 1234, "*firefox", "http://www.youtube.com/");
		//selenium = new DefaultSelenium("localhost", 1234, "*iehta", "http://www.youtube.com/");
	
		selenium.start();
	}

	@Test
	public void testYT() throws Exception {
		String vKW, vChan;
		String s1, s2, s3;
		System.out.println("Running YT1");
/*		vKW = "Selenium JUnit";
		// vChan = "QTPeLearn";
		vChan = "SeleniumeLearn"; */
		for (int i=1; i<xR_TD; i++) {
			System.out.println("TD to run for " + xTD_D[i][0]);
			if (xTD_D[i][0].equals("YT1")){
				vKW = xTD_D[i][1];
				vChan = xTD_D[i][2]; 
		//		1	Go to Youtube.com
				selenium.open("http://www.youtube.com/");
		//		2	Type the Search Keyword
				selenium.type("//*[@id=\"masthead-search-term\"]", vKW);
		//		3	Click Search Button
				selenium.click("//*[@id=\"search-btn\"]");
		//		4	Wait for Search Results
				Thread.sleep(4000);
		//		5	Capture the Channel name on Top 3 search results
				s1 = selenium.getText("//html/body/div/div[2]/div/div[4]/div/div[2]/div/div[1]/div[2]/p[2]/a");
				s2 = selenium.getText("//html/body/div/div[2]/div/div[4]/div/div[2]/div/div[2]/div[2]/p[2]/a");
				s3 = selenium.getText("//html/body/div/div[2]/div/div[4]/div/div[2]/div/div[3]/div[2]/p[2]/a");
				System.out.println("Channel 1 is " + s1);
				System.out.println("Channel 2 is " + s2);
				System.out.println("Channel 3 is " + s3);
		//		6	Compare with our expected Channel Name
		//		7	Pass if atleast one of them is a pass.
				if (s1.equals(vChan) || s2.equals(vChan) || s3.equals(vChan) ) {
					System.out.println("PASS: Channel Appears");
					xTD_D[i][4] = "Pass";
				} else {
					System.out.println("FAIL: Channel Doesnt Appear");
					xTD_D[i][4] = "Fail";
					//fail("TestNG FAIL: Channel Doesnt Appear");
				}
			}
		}
//		8	Repeat above for different Search Keywords
		System.out.println("Done with YT1");

	}

	@AfterTest
	public void tearDown() throws Exception {
		xlwrite_TD("C:/Recordings/Selenium Recordings/PL10-TestNG/YT1_Results.xls", xTD_D);
		selenium.stop();
	}
	
	public void xlRead_TD(String sPath) throws Exception{
		File myxl = new File(sPath);
		FileInputStream myStream = new FileInputStream(myxl);
		
		HSSFWorkbook myWB = new HSSFWorkbook(myStream);
		HSSFSheet mySheet = myWB.getSheetAt(1);	// Referring to 2nd sheet
		xR_TD = mySheet.getLastRowNum()+1;
		xC_TD = mySheet.getRow(0).getLastCellNum();
		xTD_D = new String[xR_TD][xC_TD];
        for (int i = 0; i < xR_TD; i++) {
	           HSSFRow row = mySheet.getRow(i);
	            for (int j = 0; j < xC_TD; j++) {
	               HSSFCell cell = row.getCell(j); // To read value from each col in each row
	               String value = cellToString(cell);
	               xTD_D[i][j] = value;
	               System.out.print(value);
	               System.out.print("    ");
	               }
	            System.out.println("");
	        }	
	}
	
	public void xlwrite_TD(String xlPath, String[][] xldata) throws Exception {
		System.out.println("Inside XL Write for Test Data Results");
    	File outFile = new File(xlPath);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet osheet = wb.createSheet("TD RESULTS");
    	for (int myrow = 0; myrow < xR_TD; myrow++) {
	        HSSFRow row = osheet.createRow(myrow);
	        for (int mycol = 0; mycol < xC_TD; mycol++) {
	        	HSSFCell cell = row.createCell(mycol);
	        	cell.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	cell.setCellValue(xldata[myrow][mycol]);
	        }
	        FileOutputStream fOut = new FileOutputStream(outFile);
	        wb.write(fOut);
	        fOut.flush();
	        fOut.close();
    	}
	}
	
	public static String cellToString(HSSFCell cell) {
		// This function will convert an object of type excel cell to a string value
	        int type = cell.getCellType();
	        Object result;
	        switch (type) {
	            case HSSFCell.CELL_TYPE_NUMERIC: //0
	                result = cell.getNumericCellValue();
	                break;
	            case HSSFCell.CELL_TYPE_STRING: //1
	                result = cell.getStringCellValue();
	                break;
	            case HSSFCell.CELL_TYPE_FORMULA: //2
	                throw new RuntimeException("We can't evaluate formulas in Java");
	            case HSSFCell.CELL_TYPE_BLANK: //3
	                result = "BLANK";
	                break;
	            case HSSFCell.CELL_TYPE_BOOLEAN: //4
	                result = cell.getBooleanCellValue();
	                break;
	            case HSSFCell.CELL_TYPE_ERROR: //5
	                throw new RuntimeException ("This cell has an error");
	            default:
	                throw new RuntimeException("We don't support this cell type: " + type);
	        }
	        return result.toString();
	    }
}
